package com.edar.web.struts2;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.dozer.DozerBeanMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.edar.util.DozerMapperSingleton;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class SimpleActionSupport extends ActionSupport {
	private static final long serialVersionUID = -6815712361545567993L;
	protected final Logger log = LoggerFactory.getLogger(SimpleActionSupport.class);
	
	@Autowired(required=false)
	protected DozerBeanMapper dozer;

	/**
	 * 绕过Template,直接输出内容的简便函数. 
	 */
	protected String render(String text, String contentType) {
		try {
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setContentType(contentType);
			response.getWriter().write(text);
		} catch (IOException e) {
			log.error(e.getMessage(), e);
		}
		return null;
	}
	/**
	 * 获取Application对象值
	 * @param key
	 * @return
	 */
	protected Object getAppValue(Object key){
		return ActionContext.getContext().getApplication().get(key);
	}
	/**
	 * 获取session对象值
	 * @param key
	 * @return
	 */
	protected Object getSessionValue(Object key){
		return ActionContext.getContext().getSession().get(key);
	}
	/**
	 * 获取getParamepters对象值
	 * @param key
	 * @return
	 */
	protected Object getParamValue(Object key){
		return ActionContext.getContext().getParameters().get(key);
	}
	/**
	 * 获取分页信息的url地址
	 * @return
	 */
	public String getNoPageUrl(){
		StringBuilder sb = new StringBuilder();
		for(String key : ActionContext.getContext().getParameters().keySet()){
			if(ignore(key))
				continue;
			sb.append(getParamStrValue(key));
		}
		return sb.toString();
	}
	private String getParamStrValue(String key){
		StringBuilder sb = new StringBuilder();
		String[] params = (String[]) getParamValue(key);
		for(String value : params){
			sb.append('&');
			sb.append(key);
			sb.append('=');
			sb.append(value);
		}
		return sb.toString();
	}
	private boolean ignore(String key){
		if("page.pageNo".equalsIgnoreCase(key)){
			return true;
		}
		if("page.pageSize".equalsIgnoreCase(key)){
			return true;
		}
		if("page.orderBy".equalsIgnoreCase(key)){
			return true;
		}
		if("page.order".equalsIgnoreCase(key)){
			return true;
		}
		return false;
	}
	/**
	 * 直接输出字符串.
	 */
	protected String renderText(String text) {
		return render(text, "text/plain;charset=UTF-8");
	}

	/**
	 * 直接输出HTML.
	 */
	protected String renderHtml(String html) {
		return render(html, "text/html;charset=UTF-8");
	}

	/**
	 * 直接输出XML.
	 */
	protected String renderXML(String xml) {
		return render(xml, "text/xml;charset=UTF-8");
	}
	
	public void initDozer() {
		// 如果Spring初始化后dozer未注入,获取无配置文件的mapper单例.
		if (dozer == null) {
			log.info("ApplicationContext中不存在dozer mapper,使用无配置文件的默认dozer.");
			dozer = DozerMapperSingleton.getInstance();
		}
	}
}
